home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
FM Towns: Free Software Collection 10
/
FM Towns Free Software Collection 10.iso
/
ms_dos
/
tool
/
txf
/
src
/
txform2.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-09-17
|
7KB
|
369 lines
/*====================================================================
*
* TXF textfile format module 2
*
*====================================================================
* copyright(C) 1992-1994 T.Nakatani
*====================================================================
*/
#include "txf.h"
void setleft()
{
int chr = NUL, pos = 0;
long iy = 1L;
char *spbuf;
tfileopen(4);
spbuf = malloc(base + 1);
if (spbuf == NULL) {
errexit("out of memory");
}
*spbuf = NUL;
while (fsize > 0) {
#ifdef DEBUG
if (viewmode > 10) {
printf("setleft->remain=%ld \r", fsize);
}
#endif
while (levelchk(iy)) {
#ifdef DEBUG
if (viewmode > 10) {
printf("setleft->remain2=%ld \r", fsize);
}
#endif
chr = NUL;
while ((chr != LF) && (fsize >= 0)) {
#ifdef DEBUG
if (viewmode > 10) {
printf("setleft->remain3=%ld \r", fsize);
}
#endif
chr = getc(input);
fsize--;
if (fsize >= 0) {
putc((char)chr, output);
}
if (fsize <= 0) {
break;
}
if (chr == LF) iy++;
}
pos = 0;
*spbuf = NUL;
if (fsize <= 0) {
break;
}
}
if (fsize <= 0) {
fputs(spbuf, output);
break;
}
if (pos >= base) {
while ((chr != LF) && (fsize >= 0)) {
chr = getc(input);
fsize--;
if (fsize >= 0) putc((char)chr, output);
if (chr == LF) iy++;
}
pos = 0;
*spbuf = NUL;
}
chr = getc(input);
fsize--;
if (chr == CR) {
fputs(spbuf, output);
*spbuf = NUL;
putc(chr, output);
chr = getc(input);
fsize--;
putc(chr, output);
pos = 0;
iy++;
}
else if (chr == 0x20) {
pos++;
strcat(spbuf, " ");
}
/* else if (chr == TAB) {
pos = ((pos / tabsize) + 1) * tabsize;
strcat(spbuf, "\t");
}
*/
else {
if (pos < base) {
fputs(spbuf, output);
pos = 0;
*spbuf = NUL;
}
if (fsize >= 0) putc((char)chr, output);
while ((chr != LF) && (fsize >= 0)) {
chr = getc(input);
fsize--;
if (fsize >= 0) putc((char)chr, output);
if (fsize <= 0) break;
if (chr == LF) iy++;
}
}
}
fflush(output);
fclose(output);
fclose(input);
free(spbuf);
tmpinfile = ((tmpinfile > 0) ? 0 : 1);
#ifdef DEBUG
if (viewmode > 2) {
fprintf(stderr, "Info:tmpinfile = %d\n",tmpinfile);
}
#endif
}
void setleft0()
{
/* unsigned long i; */
int tmpleft = maxc, chr, bs;
tfileopen(5);
fclose(output);
bs = 0;
while (fsize > 0) {
chr = getc(input);
fsize--;
if (chr == 0x20) {
bs++;
}
else if (chr == TAB) {
bs = ((bs / tabsize) + 1) * tabsize;
}
else {
if (bs < tmpleft)
tmpleft = bs;
bs = 0;
while (chr != LF) {
chr = getc(input);
fsize--;
if (fsize <= 0) {
break;
}
}
}
}
fclose(input);
base = tmpleft;
}
int putspace(int num, FILE *file)
{
int j;
for (j = 0; j < num; j++) putc(' ', file);
return (num);
}
/*
int putspace(int num, unsigned int handle)
{
int j;
char *sp;
sp = malloc(num);
for (j=0; j<num; j++) *(sp+j) = ' ';
write(handle, sp, num);
free(sp);
return (num);
}
*/
int findret(int ox, int iy, char far *rptr)
{ /* 改行コードを発見した時、連続処理するための条件 */
int retf = 0;
switch(*(rptr+2)) {
case 0x81:
if (*(rptr+3) != 0x40) break;
case 0x20:
case TAB:
case CR:
retf = 1;
}
if (putret(ox, rptr+2L) && (ox >= (right -3))) {
retf = 1;
}
if (quoteflg) {
if (quotechk(rptr+2L)) {
retf = 1;
}
}
if (levelchk(iy+1)) {
retf = 1;
}
if (ishflg) {
if (ishchk(rptr+2L) > 0) {
retf = 1;
ishline = 0;
}
}
return (retf);
}
int putret(int ox, char far *rptr)
{ /* ),.>}]。、!? 、。,.)]}>」」》』】!? 禁足処理 */
unsigned int bchr = ((*(rptr-2L) << 8) | *(rptr-1L));
unsigned int chr = ((*(rptr) << 8) | *(rptr+1L));
int retf = 1, pl = 0, pf = 0;
if (ox <= right) retf = 0;
if (jiszen(chr)) {
if (ox == right) retf = 1;
if (jstrchr(kf, chr) != NULL) {
retf = 1;
pf = 1; /* kfに存在フラグ */
}
}
else {
if (jstrchr(kf, *rptr) != NULL) {
retf = 1;
pf = 1;
}
}
if (jiszen(chr)) {
if (jstrchr(kl, chr) != NULL) {
retf = 0;
pl = 1;
}
}
else {
if (jstrchr(kl, *rptr) != NULL) {
retf = 0;
pl = 1;
}
}
if ((pf == 1) && (pl == 1)) {
if (jiszen(bchr)) {
if ((jstrchr(kl, bchr) != NULL) && (jstrchr(kf, bchr) != NULL)) {
retf = 0;
}
else {
retf = 1;
}
}
else {
if ((jstrchr(kl, *(rptr-1L)) != NULL) &&
(jstrchr(kf, *(rptr-1L)) != NULL)) {
retf = 0;
}
else {
retf = 1;
}
}
}
if (jiszen(chr) && (ox >= maxc)) retf = 1;
if (retf || (ox > maxc)) {
return (TRUE);
}
else {
return(FALSE);
}
}
int linelen(char far *ptr)
{
int len = 0;
while ((*(ptr+(long)len) != CR) && (*(ptr+(long)len) != NUL)) len++;
return (min(len, remainbyte()));
}
int levelchk(unsigned long int line)
{
if (line < starttxf) return (1);
if ((endtxf > 0) && (line > endtxf)) return (1);
return (0);
}
int ishchk(char far *ptr)
{
if (ishline == 0) {
if (matchstr(ishhead1, ptr)) {
far_strncpy(linestr, far_strstr(ptr, " ) [ ") + 5L, 6);
linestr[6] = NUL;
ishmax = atoi(linestr);
/* sscanf(far_strstr(ptr, " ) [ "), " ) [ %d", &ishmax); */
if (ishmax > 0) {
ishptr = ishchr;
ishline = 1;
goto RETL;
/* return (ishline); */
}
}
else {
goto RET0;
/* return (0); */
}
}
else if (ishline < 4) {
if ((linelen(ptr) == 78) && (*ptr == '!')) {
ishline++;
goto RETL;
/* return (ishline); */
}
else {
goto RET0;
/* return (0); */
}
}
else if (ishline == ishmax) {
ishline = 0;
ishmax = 0;
goto RET0;
/* return (0); */
}
else if ((ishline == (ishmax-3)) && (*ptr == '。')) {
ishline++;
goto RETL;
/* return (ishline); */
}
else if ((ishline == (ishmax-2)) && (*ptr == '」')) {
ishline++;
goto RETL;
/* return (ishline); */
}
else if ((linelen(ptr) == 78) && (*ptr == *ishptr)) {
ishline++;
ishptr++;
if (*ishptr == NUL) {
ishptr = ishchr;
}
goto RETL;
/* return (ishline); */
}
else {
sprintf(ishhead2, "--- ^2 (%d/%d) ---", ishline+1, ishmax);
if (matchstr(ishhead2, ptr)) {
ishline++;
goto RETL;
/* return (ishline); */
}
}
RET0:
/* printf("ISHCHK(00):%c%c\n", *ptr, *(ptr+1)); */
return (0);
RETL:
/* printf("ISHCHK(%d):%c%c\n", ishline, *ptr, *(ptr+1)); */
return (ishline);
}